package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.Area;
import com.gx.mxbbx.po.Medical;
import com.gx.mxbbx.service.MedicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class MedicalController {
    @Autowired
    private MedicalService medicalService;

    @RequestMapping("/getmedicalList")
    public String getmedicalList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String medicalName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<Medical> pageInfo = medicalService.getmedicalList(medicalName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", medicalName);
        return "/medical/medical_list";
    }

    @RequestMapping("/openmedicalAdd")
    public String openmedicalAdd(Medical medical) {
        return "/medical/medical_add";
    }

    @RequestMapping("/medicaladd")
    public String medicaladd(Medical medical, HttpServletRequest request, Model model) {
        medicalService.medicaladd(medical);
        return getmedicalList(1, 10, request, model);
    }

    @RequestMapping("/openmedicalEdit")
    public String openmedicalEdit(Integer id, Model model) {
        Medical medical = medicalService.openmedicalEdit(id);
        model.addAttribute("medical", medical);
        return "/medical/medical_edit";
    }

    @RequestMapping("/medicaledit")
    public String medicaledit(Medical medical, HttpServletRequest request, Model model) {
        medicalService.medicaledit(medical);
        return getmedicalList(1, 10, request, model);

    }

    @RequestMapping("/medicaldelete")
    public String medicaldelete(Integer id, HttpServletRequest request, Model model) {
        medicalService.medicaldelete(id);
        return getmedicalList(1, 10, request, model);
    }

}
